using NUnit.Framework;
using ValidationFramework;

namespace ExampleLibraryCSharp.BusinessBaseClasses
{
	public class ValidatableBaseExample
	{
		#region Example
		public class Person : ValidatableBase
		{

			#region Constructor

			public Person()
			{
				PropertyValidationManager.ValidateAll();
			}

			#endregion


			#region Properties

			[RequiredStringRule]
			[LengthStringRule(4)]
			public string FirstName
			{
				get;
				set;
			}


			[RequiredStringRule]
			[LengthStringRule(50, Minimum = 2)]
			public string LastName
			{
				get;
				set;
			}


			[RequiredStringRule]
			[RegexValidatorAttribute(@"\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*",
				ErrorMessage = "Invalid email format.")]
			[LengthStringRule(50)]
			public string EmailAddress
			{
				get;
				set;
			}


			#endregion
		}


		[TestFixture]
		public class ExampleUsage
		{
			[Test]
			public void RunIsValid()
			{
				var person = new Person();

				// Initially not valid
				Assert.IsFalse(person.IsValid);

				// Set FirstName, LastName and EmailAddress
				person.FirstName = "John";
				person.LastName = "Smith";
				person.EmailAddress = "John.Smith@email.com";

				// It is now valid
				Assert.IsTrue(person.IsValid);
			}


			[Test]
			public void RunValidatorResultsInError()
			{
				var person = new Person();
				// Initially there should be 3 items in ValidatorResultsInError as only 3 
				// properties have required rules.
				Assert.AreEqual(3, person.ValidatorResultsInError.Count);

				// Set LastName to a value and count goes down to 2
				person.LastName = "Smith";

				// Since IsValid has not been called the count will not have changed
				Assert.AreEqual(3, person.ValidatorResultsInError.Count);

				Assert.IsFalse(person.IsValid);
				// Since IsValid has now been called the count will have changed
				Assert.AreEqual(2, person.ValidatorResultsInError.Count);
			}


		}
		#endregion
	}
}